package tk3.project;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class SettingsActivity extends Activity {
	 
	TextView textView2;
	TextView textView3;
	TextView textView4;
	TextView textView5;
	TextView textView6;
	TextView textView7;
	TextView textView8;
	TextView textView9;
	Spinner spinner1;
	Spinner spinner2;
	Spinner spinner3;
	EditText editText1;
	EditText editText2;
	EditText editText3;
	EditText editText4;
	EditText editText5;
	CheckBox cbox1;
	CheckBox cbox2;
	Button button1;
	
	int radius;
	int radius_default = 10;
	String path;
	String path_default = Environment.getExternalStorageDirectory().getAbsolutePath()+"/dcim/";
	int interval;
	int interval_default = 1;
	String StreamName;
	String StreamName_default = "NewStream";
	String StreamPassword;
	int duration;
	int duration_default = 1;
	int Port;
	int Port_default = 80;
	String IP;
	String IP_default = "localhost";
	boolean gpsBased;
	boolean gpsBased_default = true;
	
	boolean quickStart = false;
	
	Context context;
	
	public static SettingsActivity activeInstance;
	
	public SettingsActivity(){
		super();
		activeInstance = this;
	}
	
	public void onCreate(Bundle savedInstanceState) {
		 	super.onCreate(savedInstanceState);

        	setContentView(R.layout.settings);
	        firstsave();
	        load();

	        textView2 = (TextView)findViewById(R.id.textView2);
	        textView3 = (TextView)findViewById(R.id.textView3);
	        textView4 = (TextView)findViewById(R.id.textView4);
	        textView5 = (TextView)findViewById(R.id.textView5);
	        textView6 = (TextView)findViewById(R.id.textView6);
	        textView7 = (TextView)findViewById(R.id.textView7);
	        textView8 = (TextView)findViewById(R.id.textView8);
	        textView9 = (TextView)findViewById(R.id.textView9);
	       
	        spinner1 = (Spinner)findViewById(R.id.spinner1);
	        spinner2 = (Spinner)findViewById(R.id.spinner2);
	        spinner3 = (Spinner)findViewById(R.id.spinner3);
	        
	        editText1 = (EditText)findViewById(R.id.editText1);
	        editText1.setText(path);
	        editText1.setOnEditorActionListener(new OnEditorActionListener(){

				public boolean onEditorAction(TextView v, int actionId,
						KeyEvent event) {
					path = editText1.getText().toString();
					save();
					return true;
				}
	        });
	        
	        editText2 = (EditText)findViewById(R.id.editText2);
	        editText2.setText(StreamName);
	        editText2.setEnabled(!gpsBased);
			
	        editText2.setOnEditorActionListener(new OnEditorActionListener(){

				public boolean onEditorAction(TextView v, int actionId,
						KeyEvent event) {
					StreamName = editText2.getText().toString();
					save();
					return true;
				}
	        });
	        
	        editText3 = (EditText)findViewById(R.id.editText3);
	        editText3.setText(StreamPassword);
	        editText3.setEnabled(!gpsBased);
	        
	        editText3.setOnEditorActionListener(new OnEditorActionListener(){

				public boolean onEditorAction(TextView v, int actionId,
						KeyEvent event) {
					StreamPassword = editText3.getText().toString();
					save();
					return true;
				}
	        });
	        
	        editText4 = (EditText)findViewById(R.id.editText4);
	        editText4.setText(String.valueOf(Port));
	        editText4.setOnEditorActionListener(new OnEditorActionListener(){

				public boolean onEditorAction(TextView v, int actionId,
						KeyEvent event) {
					Port = Integer.parseInt(editText4.getText().toString());
					save();
					return true;
				}
	        });
	        
	        editText5 = (EditText)findViewById(R.id.editText5);
	        editText5.setText(IP);
	        editText5.setOnEditorActionListener(new OnEditorActionListener(){

				public boolean onEditorAction(TextView v, int actionId,
						KeyEvent event) {
					IP = editText5.getText().toString();
					save();
					return true;
				}
	        });
	        
	        ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
	                this, R.array.interval_array, android.R.layout.simple_spinner_item);
	        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	        spinner1.setAdapter(adapter1);
	        spinner1.setSelection(adapter1.getPosition(String.valueOf(interval)));
	        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					String i = arg0.getItemAtPosition(arg2).toString();
					interval = Integer.parseInt(i);
					save();
				}
				public void onNothingSelected(AdapterView<?> arg0) {	
				}
			});
			
	        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
	                this, R.array.radius_array, android.R.layout.simple_spinner_item);
	        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	        spinner2.setAdapter(adapter2);
	        spinner2.setSelection(adapter2.getPosition(String.valueOf(radius)));
	        spinner2.setEnabled(gpsBased);
	        
	        spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {

				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					String i = arg0.getItemAtPosition(arg2).toString();
					radius = Integer.parseInt(i);		
				}
				public void onNothingSelected(AdapterView<?> arg0) {
				}
			});
	        
	        ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(
	                this, R.array.duration_array, android.R.layout.simple_spinner_item);
	        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	        spinner3.setAdapter(adapter3);
	        spinner3.setSelection(adapter3.getPosition(String.valueOf(duration)));
	       
	        spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {

				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					String i = arg0.getItemAtPosition(arg2).toString();
					duration = Integer.parseInt(i);
				}
				public void onNothingSelected(AdapterView<?> arg0) {
				}
			});
	        
	        cbox1 = (CheckBox)findViewById(R.id.checkBox1);
	        cbox1.setChecked(gpsBased);
	        cbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					gpsBased = isChecked;
					cbox2.setChecked(!isChecked);
					save();
					if(isChecked){
						spinner2.setEnabled(true);
						editText2.setEnabled(false);
						editText3.setEnabled(false);
					}
				}
			});
	        
	        cbox2 = (CheckBox)findViewById(R.id.checkBox2);
	        cbox2.setChecked(!gpsBased);
	        cbox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					gpsBased = !isChecked;
					cbox1.setChecked(!isChecked);
					save();
					if(isChecked){
						spinner2.setEnabled(false);
						editText2.setEnabled(true);
						editText3.setEnabled(true);
					}
				}
			});
	        
	        button1 = (Button)findViewById(R.id.button1);
	        button1.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					path = editText1.getText().toString();
					StreamName = editText2.getText().toString();
					StreamPassword = editText3.getText().toString();
					Port = Integer.parseInt(editText4.getText().toString());
					IP = editText5.getText().toString();
					save();
				}
			});
	}

	/* To load stored settings */
	
	public void load(){
		if(quickStart){
			SharedPreferences settings = context.getSharedPreferences("PicShare", 0);
			gpsBased = settings.getBoolean("gpsBased", gpsBased_default);
			interval = settings.getInt("interval", interval_default);
			radius = settings.getInt("radius", radius_default);
			path = settings.getString("path", path_default);
			StreamName = settings.getString("streamname", StreamName_default);
			StreamPassword = settings.getString("streampassword", "");
			duration = settings.getInt("duration", duration_default);
			IP = settings.getString("IP", IP_default);
			Port = settings.getInt("Port", Port_default);
			new Settings(radius, path, interval, StreamName, StreamPassword, duration, Port, IP, gpsBased);
		}
		else{
			SharedPreferences settings = getSharedPreferences("PicShare", 0);
			gpsBased = settings.getBoolean("gpsBased", gpsBased_default);
			interval = settings.getInt("interval", interval_default);
			radius = settings.getInt("radius", radius_default);
			path = settings.getString("path", path_default);
			StreamName = settings.getString("streamname", StreamName_default);
			StreamPassword = settings.getString("streampassword", "");
			duration = settings.getInt("duration", duration_default);
			IP = settings.getString("IP", IP_default);
			Port = settings.getInt("Port", Port_default);
		}
	}
	
	/* To store settings */
	
	public void save(){
		SharedPreferences settings = getSharedPreferences("PicShare", 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean("gpsBased", gpsBased);
		editor.putInt("interval", interval);
		editor.putInt("radius", radius);
		editor.putString("path", path);
		editor.putString("streamname", StreamName);
		editor.putString("streampassword", StreamPassword);
		editor.putInt("duration", duration);
		editor.putString("IP", IP);
		editor.putInt("Port", Port);
		editor.commit();
		new Settings(radius, path, interval, StreamName, StreamPassword, duration, Port, IP, gpsBased);
	}
	
	public void firstsave(){
		SharedPreferences settings = getSharedPreferences("PicShare", 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean("gpsBased", gpsBased_default);
		editor.putInt("interval", interval_default);
		editor.putInt("radius", radius_default);
		editor.putString("path", path_default);
		editor.putString("streamname", StreamName_default);
		editor.putString("streampassword", "");
		editor.putInt("duration", duration_default);
		editor.putString("IP", IP_default);
		editor.putInt("Port", Port_default);
	}
	
	public void setQS(boolean qs){
		quickStart = qs;
	}
	
	public void setContext(Context c){
		context = c;
	}
	
	public void onBackPressed(){
		save();
		finish();
	}
}